import glob
import os
import re

import openpyxl


def find_txt():
    contents = []
    i = 1
    wb = openpyxl.Workbook()
    ws = wb.create_sheet('classify')
    find_path = 'E:/garbage/garbage_classify_v2/garbage_classify_v2/train_data_v2/filename'
    os.chdir("E:/garbage/garbage_classify_v/garbage_classify_v/train_data_v")
    for file in glob.glob("*.txt"):
        strinfo = re.compile('filename')
        new_find_path = strinfo.sub(file, find_path)
        open_file = open(new_find_path, 'r', encoding='utf-8')
        for content in open_file:
            contents.append(content.split(','))
        open_file.close()
        j = 0
        ws.cell(i, j+1, contents[j][0])
        ws.cell(i, j+2, contents[j][1])
        i = i+1
        contents = []
    wb.save('C:/Users/Administrator/Desktop/2.xlsx')


find_txt()
